// pages/wo/shouyiD/shouyiD.js
import request from '../../../utils/http.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    IsLogin:false,

    IsTixian:false,//是否允许提现
    tiXianRiQiTiXing:'',

    personId: 0,
    dianPuId:0,

    // 佣金


    zongShouRu:0,//总输入
    daiTiQu:0,//
    daiFenPei:0,
    monthRuZhang:0,
    monthTiQu:0,

    tabList: ["佣金明细", "提现记录"],
    tabIndex: 0,

    ruZhangList: [],
    tiQuList: [],
    monthRuZhang: 0, //本月入账
    monthTiQu: 0, //本月出账



    date: '',
    mouth: '',
    isDate: false,
    windowHeight: '',
    isgetData: false //false没有本地数据；true 有数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      personId: wx.getStorageSync(app.globalData.PERSON_ID) == "" ? 0 : wx.getStorageSync(app.globalData.PERSON_ID),
      IsLogin: wx.getStorageSync(app.globalData.LOGINSTATUS),
      dianPuId:wx.getStorageSync(app.globalData.SHOPID),
    })

    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight - (550/750 * res.windowWidth),
        })
      },

    })
    //console.log(wx.getStorageSync(app.globalData.screenHeight)-300)

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

    var date = new Date()
    var zero = '0'
    var mouthStr = date.getMonth() + 1

    if (mouthStr < 10) {
      mouthStr = zero.concat(mouthStr)
    }
    this.setData({
      mouth: date.getFullYear() + '-' + mouthStr
    })

    this.getShouYeMingXi()


  },

  //根据条件，查询收益
  getShouYeMingXi: function () {

    var that = this
    wx.showLoading({
      title: '加载中...',
    })

    let data = {
      dianPuId: that.data.dianPuId,
      dateStr: that.data.date == '' ? that.data.mouth : that.data.date
    }

    request.get('yongJin/dianPuShouYe', data, res => {
    //  console.log("店铺收益:"+ JSON.stringify(res.data))
      that.setData({
        zongShouRu:res.data.zongShouRu,
        daiTiQu:res.data.daiTiQu,
        daiFenPei:res.data.daiFenPei,      
        monthRuZhang:res.data.monthRuZhang,
        monthTiQu:res.data.monthTiQu,    
        IsTixian:res.data.shiFouKeYiTiXian==1?true:false,
        tiXianRiQiTiXing:res.data.tiXianRiQiTiXing
      })

      res.data.ruZhangList.forEach((item, index) => {
          if(item.zhuangTai==1){         
            item.imgUrl = '/image/money_icon11.png';         
          }else if(item.zhuangTai==2){
            item.imgUrl = '/image/money_icon12.png';
          }else{
            item.imgUrl = '/image/money_icon10.png';
          }
      })

      res.data.tiQuList.forEach((item, index) => {
        if(item.zhuangTai==1){
          item.imgUrl = '/image/money_icon13.png';
        }else if(item.zhuangTai==2){
          item.imgUrl = '/image/money_icon14.png';
        }else{
          item.imgUrl = '/image/money_icon10.png';
        }
    })

      that.setData({
        ruZhangList:res.data.ruZhangList,
        tiQuList:res.data.tiQuList,
      })

      wx.hideLoading()

    }, err => {
      wx.showToast({
        title: "获取收益信息失败！", // 提示的内容
        icon: "none", // 图标，默认success
        duration: 3000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })

  },


  //提现
  bindDetailTap: function () {
    if(this.data.IsTixian){
      wx.navigateTo({
        url: '/pkg_wo/pages/cashOut/cashOut?leiXing=3&daiTiQuShouYi='+this.data.daiTiQu,
      })
    }else{
      wx.showToast({
        title: this.data.tiXianRiQiTiXing, // 提示的内容
        icon: "none", // 图标，默认success
        duration: 3000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    }

  },

  //切换tab
  bindTabTap: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex: index
    })
    if (this.data.tabIndex == 0) {
      this.setData({
        isgetData: this.data.ruZhangList.length > 0 ? true : false
      })

    } else if (this.data.tabIndex == 1) {
      this.setData({
        isgetData: this.data.tiQuList.length > 0 ? true : false
      })
    } 
  },


  //日选择器
  bindDateChange(e) {
    this.setData({
      date: e.detail.value,
      isDate: true,
    })
    this.getShouYeMingXi()
  },


  //月选择器
  bindMouthChange(e) {
    this.setData({
      mouth: e.detail.value,
      isDate: false,
    })
    this.getShouYeMingXi()
  },

  //清空日期
  clearnDate: function () {
    this.setData({
      date: '',
      isDate: false,
      tabIndex: 0
    })

    if (this.data.tabIndex == 0) {
      this.setData({
        isgetData: this.data.ruZhangList.length > 0 ? true : false
      })

    } else if (this.data.tabIndex == 1) {
      this.setData({
        isgetData: this.data.chuZhangList.length > 0 ? true : false
      })
    } else if (this.data.tabIndex == 2) {
      this.setData({
        isgetData: this.data.houKouList.length > 0 ? true : false
      })

    }

    var date = new Date()
    var zero = '0'
    var mouthStr = date.getMonth() + 1

    if (mouthStr < 10) {
      mouthStr = zero.concat(mouthStr)
    }
    this.setData({
      mouth: date.getFullYear() + '-' + mouthStr
    })

    this.getShouYeMingXi()
  }
})